﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AcDotNetTool;
using Autodesk.AutoCAD.EditorInput;
namespace _07文字对象
{
    public class Class1
    {
        [CommandMethod("TextDemo")]
        public void TextDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            
            Line[] line = new Line[10];
            Point3d[] pt1 = new Point3d[10];
            Point3d[] pt2 = new Point3d[10];
            for (int i = 0; i < line.Length; i++)
            {
                pt1[i] = new Point3d(50, 50 + 20 * i, 0);
                pt2[i] = new Point3d(150, 50 + 20 * i, 0);
                line[i] = new Line(pt1[i], pt2[i]);
            }
            db.AddEnityToModelSpace(line);


            DBText text0 = new DBText();
            text0.Position = pt1[0];
            text0.TextString = "你好，C#.Net!";
            text0.Height = 5;
            text0.Rotation = Math.PI * 0.5;
            //text0.IsMirroredInX = true;
            db.AddEnityToModelSpace(text0);

            DBText text1 = new DBText();
            text1.Position = pt1[1];
            text1.TextString = "你好，C#.Net!";
            //text1.IsMirroredInY = true;
            text1.Height = 20;

            db.AddEnityToModelSpace(text1);

            DBText text2 = new DBText();
            text2.Position = pt1[2];
            text2.Height = 10;
            text2.TextString = "你好，C#.Net!";
            //text2.Rotation = Math.PI * 0.1;
            //text2.IsMirroredInX = true;
            //text2.IsMirroredInY = true;
            db.AddEnityToModelSpace(text2);
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                DBText text3 = new DBText();
                text3.Position = pt1[3];
                text3.TextString = "你好，C#.Net! 3";
                text3.Height = 10;
                text3.HorizontalMode = TextHorizontalMode.TextAlign;
                text3.VerticalMode = TextVerticalMode.TextBase;
                text3.AlignmentPoint = text3.Position;
                db.AddEnityToModelSpace(text3);

                DBText text4 = new DBText();
                text4.Position = pt1[4];
                text4.TextString = "你好，C#.Net! 4";
                text4.Height = 10;
                text4.HorizontalMode = TextHorizontalMode.TextCenter;
                text4.VerticalMode = TextVerticalMode.TextBottom;
                text4.AlignmentPoint = text4.Position;
                db.AddEnityToModelSpace(text4);

                DBText text5 = new DBText();
                text5.Position = pt1[5];
                text5.TextString = "你好，C#.Net! 5";
                text5.Height = 10;
                text5.HorizontalMode = TextHorizontalMode.TextFit;
                text5.VerticalMode = TextVerticalMode.TextTop;
                text5.AlignmentPoint = text5.Position;
                db.AddEnityToModelSpace(text5);

                DBText text6 = new DBText();
                text6.Position = pt1[6];
                text6.TextString = "你好，C#.Net! 6";
                text6.Height = 10;
                text6.HorizontalMode = TextHorizontalMode.TextLeft;
                text6.VerticalMode = TextVerticalMode.TextVerticalMid;
                //text6.AlignmentPoint = text6.Position;
                db.AddEnityToModelSpace(text6);

                DBText text7 = new DBText();
                text7.Position = pt1[7];
                text7.TextString = "你好，C#.Net! 7";
                text7.Height = 10;
                text7.HorizontalMode = TextHorizontalMode.TextMid;
                text7.AlignmentPoint = text7.Position;
                text7.VerticalMode = TextVerticalMode.TextVerticalMid;
                db.AddEnityToModelSpace(text7);

                DBText text8 = new DBText();
                text8.Position = pt1[8];
                text8.TextString = "你好，C#.Net! 8";
                text8.Height = 10;
                text8.HorizontalMode = TextHorizontalMode.TextRight;
                text8.VerticalMode = TextVerticalMode.TextBottom;
                text8.AlignmentPoint = text8.Position;
                db.AddEnityToModelSpace(text8);
                trans.Commit();
            }
            
            
        }

        [CommandMethod("TextDemo2")]
        public void TextDemo2()
        {
            //"面积≈%%U100%%Um²"
            Database db = HostApplicationServices.WorkingDatabase;
            DBText text = new DBText();
            text.Position = new Point3d(100, 100, 0);
            //text.TextString = "面积≈%%U100%%Um²";
            text.TextString = "李小科" + TextTool.TextSpecialSymbol.Alpha + TextTool.TextSpecialSymbol.Tolerance + "0.006"
                 + TextTool.TextSpecialSymbol.Underline + "C#.net" + TextTool.TextSpecialSymbol.Underline;
            text.Height = 10;
            db.AddEnityToModelSpace(text);
        }

        [CommandMethod("MTextDemo")]
        public void MTextDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            MText mtext1 = new MText();
            mtext1.Location = new Point3d(100, 100, 0);
            mtext1.Contents = "你好,AutoCAD二次开发,可以提供我们的工作效率,节省我们的时间,所以我们才来学习它.";


            MText mtext2 = new MText();
            mtext2.Location = new Point3d(200, 100, 0);
            mtext2.Contents = "222你好,AutoCAD二次开发,可以提供我们的工作效率,\n节省我们的时间,所以我们才来学习它.";

            MText mtext3 = new MText();
            mtext3.Location = new Point3d(300, 100, 0);
            mtext3.Contents = "33你好,AutoCAD二次开发,可以提供我们的工作效率,节省我们的时间,所以我们才来学习它.";
            mtext3.Width = 5;

            MText mtext4 = new MText();
            mtext4.Location = new Point3d(400, 100, 0);
            mtext4.Contents = "44你好,AutoCAD二次开发,可以提供我们的工作效率,节省我们的时间,所以我们才来学习它.";
            mtext4.Width = 5;
            mtext4.Height = 0.5;

            MText mtext5 = new MText();
            mtext5.Location = new Point3d(500, 100, 0);
            mtext5.Contents = "55你好,AutoCAD二次开发,可以提供我们的工作效率,节省我们的时间,所以我们才来学习它.";
            mtext5.Width = 5;
            mtext5.TextHeight = 20;

            MText mtext6 = new MText();
            mtext6.Location = new Point3d(600, 100, 0);
            mtext6.Contents = "66你好,AutoCAD二次开发,可以提供我们的工作效率,节省我们的时间,所以我们才来学习它.";
            mtext6.Rotation = Math.PI * 0.25;

            db.AddEnityToModelSpace(mtext1, mtext2, mtext3, mtext4, mtext5, mtext6);
        }

        [CommandMethod("MTextDemo2")]
        public void MTextDemo2()
        {
           // "\\A1;∅20{\\H0.5x;\\SH7/P7;}"
            Database db = HostApplicationServices.WorkingDatabase;
            MText mtext = new MText();
            //mtext.Contents = "\\A1;∅20{\\H0.5x;\\SH7/P7;}";

            //mtext.Contents = string.Format("\\A1;{0}{1}\\H{2}x;\\S{3}{4}{5};{6}", "∅20","{",0.2,"李小科","#","度娘","}");

            string str = "你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好" + TextTool.TextSpecialSymbol.Diameter
                + "50";
            str += TextTool.StackMText(str, 0.4, "+0.5", TextTool.MTextStackType.Tolerance, "-0.1");
            str += "你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好";
            str += MText.ParagraphBreak + "李小科";
            mtext.Contents = str;
            mtext.Location = new Point3d(100, 100, 0);
            mtext.TextHeight = 10;
            mtext.Width = 100;
            db.AddEnityToModelSpace(mtext);
        }

        [CommandMethod("PickMTextDemo")]
        public void PickMTextDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptEntityResult per = ed.GetEntity("\n 请选择多行文字:");
            if (per.Status != PromptStatus.OK) return;

            Entity ent;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                ent = (Entity)per.ObjectId.GetObject(OpenMode.ForRead);
            }
            MText mtext = (MText)ent;

        }
    }
}
